<?php

/**
 * synchronize actions.
 *
 * @package    sf_sandbox
 * @subpackage synchronize
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class synchronizeActions extends autosynchronizeActions
{
 public function executeIndex()
  {
    return $this->forward('synchronize', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();


    // pager
    $this->pager = new sfPropelPager('RingtoneSynchronize', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/ringtone_synchronize')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/ringtone_synchronize');
    }
  }

  public function executeCreate()
  {
    return $this->forward('synchronize', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('synchronize', 'edit');
  }

  public function executeEdit()
  {
    $this->ringtone_synchronize = $this->getRingtoneSynchronizeOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateRingtoneSynchronizeFromRequest();

      $this->saveRingtoneSynchronize($this->ringtone_synchronize);

      $this->setFlash('notice', 'Thông tin đã được cập nhật');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('synchronize/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('synchronize/list');
      }
      else
      {
        return $this->redirect('synchronize/edit?ring_id='.$this->ringtone_synchronize->getRingId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->ringtone_synchronize = RingtoneSynchronizePeer::retrieveByPk($this->getRequestParameter('ring_id'));
    $this->forward404Unless($this->ringtone_synchronize);

    try
    {
      $this->deleteRingtoneSynchronize($this->ringtone_synchronize);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Ringtone synchronize. Make sure it does not have any associated items.');
      return $this->forward('synchronize', 'list');
    }

    return $this->redirect('synchronize/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->ringtone_synchronize = $this->getRingtoneSynchronizeOrCreate();
    $this->updateRingtoneSynchronizeFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  // thangtv2 code
  public function executeSyned()
  {
  	$ringtone_synchronize = new RingtoneSynchronize();
  	$ringtone_synchronize->setRingId($this->getRequestParameter('ring_id'));
	$ringtone_synchronize->setSysDate('now');
  	RingtoneSynchronizePeer::doUpdate($ringtone_synchronize);
  	return $this->redirect('synchronize/list');
  	
    
  }
  

  protected function saveRingtoneSynchronize($ringtone_synchronize)
  {

  }

  protected function deleteRingtoneSynchronize($ringtone_synchronize)
  {
    $ringtone_synchronize->delete();
  }

  protected function updateRingtoneSynchronizeFromRequest()
  {
    $ringtone_synchronize = $this->getRequestParameter('ringtone_synchronize');

    if (isset($ringtone_synchronize['sys_date']))
    {
      if ($ringtone_synchronize['sys_date'])
      {
        try
        {
          $dateFormat = new sfDateFormat($this->getUser()->getCulture());
                              if (!is_array($ringtone_synchronize['sys_date']))
          {
            $value = $dateFormat->format($ringtone_synchronize['sys_date'], 'I', $dateFormat->getInputPattern('g'));
          }
          else
          {
            $value_array = $ringtone_synchronize['sys_date'];
            $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
          }
          $this->ringtone_synchronize->setSysDate($value);
        }
        catch (sfException $e)
        {
          // not a date
        }
      }
      else
      {
        $this->ringtone_synchronize->setSysDate(null);
      }
    }
    if (isset($ringtone_synchronize['ring_name']))
    {
      $this->ringtone_synchronize->setRingName($ringtone_synchronize['ring_name']);
    }
    if (isset($ringtone_synchronize['ring_link']))
    {
      $this->ringtone_synchronize->setRingLink($ringtone_synchronize['ring_link']);
    }
  }

  protected function getRingtoneSynchronizeOrCreate($ring_id = 'ring_id')
  {
    if (!$this->getRequestParameter($ring_id))
    {
      $ringtone_synchronize = new RingtoneSynchronize();
    }
    else
    {
      $ringtone_synchronize = RingtoneSynchronizePeer::retrieveByPk($this->getRequestParameter($ring_id));

      $this->forward404Unless($ringtone_synchronize);
    }

    return $ringtone_synchronize;
  }

  protected function processFilters()
  {
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/ringtone_synchronize/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/ringtone_synchronize/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/ringtone_synchronize/sort'))
    {
      $this->getUser()->setAttribute('sort', 'sys_date', 'sf_admin/ringtone_synchronize/sort');
      $this->getUser()->setAttribute('type', 'desc', 'sf_admin/ringtone_synchronize/sort');
    }
  }

  protected function addFiltersCriteria($c)
  {
  }

  protected function addSortCriteria($c)
  {
  	// thangtv2 add
  	 $c->add(RingtoneSynchronizePeer::SYS_DATE, null);
  	 $c->addJoin(RingtonePeer::ID, RingtoneSynchronizePeer::RING_ID);
  	 $c->addJoin(RingtoneExtraPeer::RINGTONE_ID, RingtoneSynchronizePeer::RING_ID);
  	 
  	 $c->add(RingtonePeer::RBT_STATUS,1);
  	
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/ringtone_synchronize/sort'))
    {
      $sort_column = RingtoneSynchronizePeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/ringtone_synchronize/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'ringtone_synchronize{ring_id}' => 'ID:',
      'ringtone_synchronize{sys_date}' => 'Ngày đồng bộ:',
      'ringtone_synchronize{ring_name}' => 'Tên bài nhạc:',
      'ringtone_synchronize{ring_link}' => 'Ring link:',
    );
  }
}
